package com.zbp.service.impl;

import com.zbp.model.vo.UserVO;
import com.zbp.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author niuben
 */
@Service
public class SecurityUserDetailsService implements UserDetailsService {

    @Autowired
    private IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询用户信息
        //将数据封装成UserDetails
        UserVO userVO = userService.get(username);
        //如果没有查询到用户，就抛出异常
        if (Objects.isNull(userVO)) {
            throw new RuntimeException("用户名或者密码错误！");
        }
        return userVO;
    }
}

